#!/usr/bin/env python
# encoding: utf-8
#
# @author: zhujianhua
#
import sys
import os
import platform
import schedule
import time

sys.path.append('./helper')
from ConfigReader import ConfigReader
import InfoMonConstants
from LoggerHelper import LoggerHelper

sys.path.append('./task')
from ExecuteScripts import ExecuteScripts
from Collector import Collector

# 初始化常量
def init():
    try:
        InfoMonConstants.CONFPATH = os.path.join(os.getcwd(), "etc", 'mon.cfg')
        cfg = ConfigReader(InfoMonConstants.CONFPATH)
        InfoMonConstants.HOST = cfg.get('oracle', 'host')
        InfoMonConstants.PORT = cfg.get('oracle', 'port')
        InfoMonConstants.USER = cfg.get('oracle', 'user')
        InfoMonConstants.PASSWORD = cfg.get('oracle', 'password')
        InfoMonConstants.SID = cfg.get('oracle', 'sid')
        InfoMonConstants.ENDPOINT = cfg.get('default', 'endpoint')
        InfoMonConstants.FALCON_CLINENT = cfg.get('default', 'falcon_client')
        InfoMonConstants.EXTEND_CLIENT = cfg.get('default', 'extend_client')
        InfoMonConstants.LOG_FILE = cfg.get('default', 'log_file')
        InfoMonConstants.LOG_LEVEL = cfg.get('default', 'log_level')
        InfoMonConstants.REMOTEMONITOR = cfg.get('default', 'remote_monitor')

        InfoMonConstants.LINUXHOST = cfg.get('linux', 'host')
        InfoMonConstants.LINUXUSER = cfg.get('linux', 'user')
        InfoMonConstants.LINUXPASSWORD = cfg.get('linux', 'password')

        InfoMonConstants.WINDOWSHOST = cfg.get('windows', 'host')
        InfoMonConstants.WINDOWSUSER = cfg.get('windows', 'user')
        InfoMonConstants.WINDOWSPASSWORD = cfg.get('windows', 'password')

    except Exception as e:
        logger = LoggerHelper("Main")
        logger.error("初始化配置信息出错 :" + str(e))


def Main():
    logger = LoggerHelper("Main")
    logger.info("Starting ...")

    # 执行脚本
    exeCute = ExecuteScripts()
    exeCute.execute()
    #数据库信息收集,这里不用区分操作系统
    c = Collector()
    c.run()
    #系统信息收集就需要区分操作系统，
    systemType = platform.system()
    if 'true' == InfoMonConstants.REMOTEMONITOR.lower():
        if InfoMonConstants.LINUXHOST and InfoMonConstants.LINUXUSER and InfoMonConstants.LINUXPASSWORD:
            from LinuxsystemInfo import LinuxsystemInfo
            systemInfo = LinuxsystemInfo(InfoMonConstants.LINUXHOST, InfoMonConstants.LINUXUSER, InfoMonConstants.LINUXPASSWORD)
            systemInfo.run()
        if InfoMonConstants.WINDOWSHOST and InfoMonConstants.WINDOWSUSER and InfoMonConstants.WINDOWSPASSWORD:
            from WinSystemInfo import WinSystemInfo
            wsInfo = WinSystemInfo(InfoMonConstants.WINDOWSHOST, InfoMonConstants.WINDOWSUSER, InfoMonConstants.WINDOWSPASSWORD)
            wsInfo.run()
    elif "Windows" == systemType :
        #windows系统信息收集
        from WinSystemInfo import WinSystemInfo
        wsInfo = WinSystemInfo()
        wsInfo.run()
    elif "Linux" == systemType:
        #linux系统信息收集
        from LinuxsystemInfo import LinuxsystemInfo
        systemInfo = LinuxsystemInfo()
        systemInfo.run()
    else :
        logger.error("不支持操作系统类型:" + systemType)

    logger.info("end .")


def getTopSqlInfo():
    c = Collector()
    c.runTopSQL()


schedule.every(1).minutes.do(Main)
schedule.every(5).minutes.do(getTopSqlInfo)

if __name__ == '__main__':
    init()
    Main()
    getTopSqlInfo()
    while True:
        schedule.run_pending()
        time.sleep(1)
